//
//  PurchasePowerAlertController.swift
//  SCA
//
//  Created by tjun on 2025/9/12.
//

import UIKit

class PurchasePowerAlertController: UIViewController {

    private var maskView: UIButton!
    private var contentView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        view.backgroundColor = .clear
        
        maskView = UIButton(frame: view.bounds).addTo(view)
        maskView.backgroundColor = .black.withAlphaComponent(0)
        
        let contentW = view.width - 80
        let contentH = screenHeight * 0.7
        
        contentView = UIView().addTo(view)
        contentView.backgroundColor = UIColorMake(0x212529)
        contentView.frame = CGRectMake((view.width - contentW) * 0.5, -contentH , contentW, contentH)
        contentView.addCorner(radius: 15)
        
        let scrollLabel = ScrollableLabel()
        scrollLabel.textColor = .white
        
        let text = "如何倍增收益？\n\n如何加速算力、获得更多收益？\n\n想在智云时代开启创富之旅？成为动态用户，享有更多加速算力和多重收益！\n 一、直推收益（最多40%）\n推荐500美金起，即可获得固定比例收益：\n•第1位13%，\n•第2位16%，\n每位递增3%，第10位起享最高40%！\n二、每日矩阵收益（限≥5G）\n参与5G及以上进阶包，系统自动排位上下25层，总算力×1%×$1，每天轻松收入！\n\n三、每日级差收益（最高46%）\n升级星级，团队越强，级差越大，每日可获得最高奖励！\n\n四、每日平级收益（S6起享）\n当你和团队成员同级，还可获得1%平级收益，让收入更稳更持久！\n\n成为动态用户小贴士：\n直推越多 ➜ 算力越高 ➜ 收益越大\n\n别再观望，让加速的算力为你启动财富引擎！".localizedString
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 3          // 行间距
        paragraphStyle.paragraphSpacing = 5    // 段落之间的间距
        paragraphStyle.alignment = .left        // 对齐方式

        scrollLabel.attributedText = NSAttributedString(
            string: text,
            attributes: [
                .font: UIFont.systemFont(ofSize: 16),
                .foregroundColor: UIColor.white,
                .paragraphStyle: paragraphStyle
            ]
        )
        
        let closeButton = UIButton(type: .system)
        closeButton.setImage(UIImage(systemName: "xmark"), for: .normal)
        closeButton.tintColor = .white
        closeButton.addTarget(self, action: #selector(closeButtonTapped), for: .touchUpInside)
        contentView.addSubview(closeButton)
        closeButton.snp.makeConstraints { make in
            make.right.equalTo(-20)
            make.top.equalToSuperview().offset(10)
            make.width.height.equalTo(30)
        }

        let confirmButton = MainButton(type: .system, title: "OK", font: UIFontMake(16), textColor: .white)
        confirmButton.addTo(contentView)
        confirmButton.addCorner(radius: 10)
        confirmButton.addTarget(self, action: #selector(confirmButtonTapped), for: .touchUpInside)
        confirmButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.height.equalTo(AppStyle.size.smallButtonHeight.scaled)
            make.bottom.equalToSuperview().offset(-30)
        }
        
        contentView.addSubview(scrollLabel)
        scrollLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.top.equalTo(40)
            make.bottom.equalTo(confirmButton.snp.top).offset(-30)
        }
    }
    
    @objc func confirmButtonTapped() {
        dismiss()
    }
    
    @objc func closeButtonTapped() {
        dismiss()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func showIn(_ controller: UIViewController) {
        controller.present(self, animated: false, completion: nil)
    }
 
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        modalPresentationStyle = .custom
    }
 
    private func dismiss(completion: (() -> Void)? = nil) {
        UIView.animate(withDuration: 0.25) {
            self.contentView.alpha = 0
            self.maskView.backgroundColor = .black.withAlphaComponent(0)
            self.contentView.y = self.contentView.y + 100
        } completion: { finishi in
            self.dismiss(animated: false, completion: completion)
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        UIView.animate(withDuration: 0.75, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.8, options: .curveEaseIn) {
            self.maskView.backgroundColor = .black.withAlphaComponent(0.5)
            self.contentView.y = (self.view.height - self.contentView.height) * 0.5 - 35
        }
    }
    

}
